package com.MRYIN.controller;

import com.MRYIN.mapper.KWMapper;
import com.MRYIN.mapper.PaymentsMapper;
import com.MRYIN.pojo.KW;
import com.MRYIN.pojo.Orders;
import com.MRYIN.pojo.Payments;
import com.MRYIN.pojo.Task;
import com.MRYIN.service.OrdersService;
import com.MRYIN.service.TaskService;
import com.MRYIN.util.DataGridView;
import com.MRYIN.util.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @program: SHYC_03
 * @description: 款项的控制器
 * @author: Mr.YIN
 * @create: 2021-02-22 17:25
 **/
@Controller
@RequestMapping("/KW")
public class KWController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private KWMapper kwMapper;

    @Autowired
    private PaymentsMapper paymentsMapper;

    @Autowired
    private TaskService taskService;


    @RequestMapping("/toKW")
    public String toKW(Integer PO_ID, HttpSession session) {
        System.out.println(PO_ID);
        //Orders orders = ordersService.queryOrderByPO_ID(PO_ID);
        //System.out.println(orders);
        session.setAttribute("PO_ID", PO_ID);
        return "KW/KWList";
    }

    /*@RequestMapping("/")
    public */

    @RequestMapping("/KWList")
    public String KWList(HttpSession session, Model model) {
        Integer PO_ID = (Integer) session.getAttribute("PO_ID");
        System.out.println(PO_ID);
        Orders orders = ordersService.queryOrderByPO_ID(PO_ID);
        model.addAttribute("orders", orders);
        session.setAttribute("orders", orders);
        return "KW/KWList";
    }

    @RequestMapping("/KWTable")
    @ResponseBody
    public Map<String, Object> KWTable(@RequestParam("page") Integer page, @RequestParam("limit") Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        List list = new LinkedList();
        System.out.println(page);
        System.out.println(pageSize);
        for (int i = 1; i < 8; i++) {
            List<KW> kws = kwMapper.queryAllKWByKW_CODEOrderByKW_ID(i);
            for (KW kw : kws) {
                list.add(kw);
            }
        }
        map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("data", list);
        /*Set<Map.Entry<String, Object>> entries = map.entrySet();
//        for (Map.Entry<String,Object> entry : entries){
//            System.out.println(entry.getKey());
//            System.out.println(entry.getValue());
//        }
        entries.forEach(entry->{
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        });*/
        return map;
    }

    @RequestMapping("/KWTask/{PO_PRJCODE}")
    @ResponseBody
    public DataGridView kwTask(@PathVariable String PO_PRJCODE, @RequestParam("page") Integer page, @RequestParam("limit") Integer pageSize) {
        System.out.println(PO_PRJCODE);
        System.out.println(page);
        System.out.println(pageSize);
        List<Payments> payments = paymentsMapper.queryAllPaymentsByPAY_PRJCODE(PO_PRJCODE);
        return new DataGridView(0, "查询成功", payments);
    }

    @RequestMapping("/KWSelectKW_CLASS")
    @ResponseBody
    public Map<String, Object> kwSelectCLASS() {
        Map<String, Object> map = new HashMap<>();
        List list = new LinkedList();
        for (int i = 1; i < 8; i++) {
            List<KW> kws = kwMapper.queryAllKWByKW_CODEOrderByKW_ID(i);
            for (KW kw : kws) {
                list.add(kw);
            }
        }
        map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("data", list);
        /*Set<Map.Entry<String, Object>> entries = map.entrySet();
//        for (Map.Entry<String,Object> entry : entries){
//            System.out.println(entry.getKey());
//            System.out.println(entry.getValue());
//        }
        entries.forEach(entry->{
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        });*/
        return map;
    }

    @RequestMapping("/KWSelectKW_NAME")
    @ResponseBody
    public DataGridView kwSelectNAME(Integer value) {
        System.out.println(value);
        List<KW> kws = kwMapper.queryAllKWByKW_CODE(value);
        return new DataGridView(0, "查询成功", kws);
    }

    @RequestMapping("/payMents")
    @ResponseBody
    public ResultObj payMents(String PAY_PRJCODE, String PAY_CODE, String PAY_NAME, Double PAY_AMOUNT) {
        System.out.println(PAY_AMOUNT);
        System.out.println(PAY_CODE);
        System.out.println(PAY_NAME);
        System.out.println(PAY_PRJCODE);
        Payments payments = new Payments();
        payments.setPAY_PRJCODE(PAY_PRJCODE);
        payments.setPAY_CODE(PAY_CODE);
        payments.setPAY_NAME(PAY_NAME);
        payments.setPAY_AMOUNT(PAY_AMOUNT);
        try {
            paymentsMapper.insertPayment(payments);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 通过传递到后台的代码，查询出来与之对应的所有KW_NAME
     *
     * @param pay_CODE kw表中的KW_CODE
     * @return
     */
    @RequestMapping("/KW_NAMELIST")
    @ResponseBody
    public DataGridView KW_NAMELIST(Integer pay_CODE, HttpSession session) {
        System.out.println("我是/KW_NAMELIST=>" + pay_CODE);
        session.setAttribute("pay_CODE", pay_CODE);
        List<KW> kws = kwMapper.queryAllKWByKW_CODE(pay_CODE);
        return new DataGridView(0, "查询成功");
    }

    @RequestMapping("/KW_NAMELIST1")
    @ResponseBody
    public DataGridView KW_NAMELIST1(Integer pay_CODE, HttpSession session) {
        Integer payCode = (Integer) session.getAttribute("pay_CODE");
        System.out.println("我是/KW_NAMELIST1=>" + payCode);
        List<KW> kws = kwMapper.queryAllKWByKW_CODE(payCode);
        return new DataGridView(0, "查询成功", kws);
    }

    /**
     * 把下拉选择框中选择的值传到后台，然后根据这个值查询出来所有的子选项
     *
     * @return
     */
    @RequestMapping("/addKWTask")
    @ResponseBody
    public ResultObj addKWTask(HttpSession session,
                                  Integer kw_ID,
                                  Integer kw_CODE,
                                  String kw_NAME,
                                  String kw_TYPE,
                                  String kw_CLASS,
                                  String kw_CONTENT,
                                  String task_RESPONER) {
        System.out.println(kw_ID);
        System.out.println(kw_CODE);
        System.out.println(kw_NAME);
        System.out.println(kw_TYPE);
        System.out.println(kw_CLASS);
        System.out.println(kw_CONTENT);
        System.out.println(task_RESPONER);
        Task task = new Task();
        Orders orders = (Orders) session.getAttribute("orders");
        String po_prjcode = orders.getPO_PRJCODE();
        task.setTK_PRJCode(po_prjcode);
        task.setTK_TaskType(kw_TYPE);
        task.setTK_CODE("P" + kw_CODE);
        task.setTK_SONCODE("S" + kw_ID);
        task.setTK_NAME(kw_NAME);
        task.setTK_PreTask(kw_CONTENT);
        task.setTK_RESPONER(task_RESPONER);
        task.setTK_DETAILS(kw_NAME);
        try {
            taskService.addKWTask(task);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    @RequestMapping("/CheckKWTask")
    @ResponseBody
    public DataGridView checkKWTask(String PAY_PRJCODE  , Integer PAY_CODE , HttpSession session){
        System.out.println(PAY_PRJCODE);
        System.out.println(PAY_CODE);
        session.setAttribute("PAY_CODE",PAY_CODE);
        //taskService.getTaskByPRJCODE_CODE_SONCODE(PAY_PRJCODE)
        List<Task> taskByPRJCODE_code_soncode = taskService.getTaskByPRJCODE_CODE_SONCODE(PAY_CODE);
        for (Task task : taskByPRJCODE_code_soncode) {
            System.out.println(task);
        }
        return new DataGridView(0,"查询成功");
    }

    @RequestMapping("/CheckKWTask1")
    @ResponseBody
    public DataGridView checkKWTask1(HttpSession session){
        Integer pay_code = (Integer) session.getAttribute("PAY_CODE");
        //taskService.getTaskByPRJCODE_CODE_SONCODE(PAY_PRJCODE)
        List<Task> taskByPRJCODE_code_soncode = taskService.getTaskByPRJCODE_CODE_SONCODE(pay_code);
        for (Task task : taskByPRJCODE_code_soncode) {
            System.out.println(task);
        }
        return new DataGridView(0,"查询成功",taskByPRJCODE_code_soncode);
    }
}
